package cn.edu.ctbu.datajpa01.service.impl;

import cn.edu.ctbu.datajpa01.domain.Schedule;
import cn.edu.ctbu.datajpa01.domain.Student;
import cn.edu.ctbu.datajpa01.repository.ScheduleRepository;
import cn.edu.ctbu.datajpa01.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author losen
 * @version 1.0
 * @DateTime 2023/1/10 15:40
 */

@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    ScheduleRepository scheduleRepository;

    public List<Schedule> findAll(){
        return scheduleRepository.findAll();
    }

    /**
     * 根据课程名称查询
     * @param name
     * @return
     */
    @Override
    public List<Schedule> findByName(String name) {
        return scheduleRepository.findByName(name);
    }

    /**
     * 查询全部并分页
     * @param pageable
     * @return
     */

    @Override
    public Page<Schedule> findAll(Pageable pageable) {
        return scheduleRepository.findAll(pageable);
    }

    @Override
    public Page<Schedule> findAll(Example<Schedule> studentExample, Pageable pageable) {
        return scheduleRepository.findAll(studentExample,pageable);
    }


    /**
     * 新增课程
     * @param schedule
     * @return
     */
    @Override
    public Schedule insert(Schedule schedule) {
        return  scheduleRepository.save(schedule);
    }

    @Override
    public Schedule getById(Integer id) {
        Schedule schedule=scheduleRepository.findById(id).orElse(null);
        return  schedule;
    }

    @Override
    public Schedule update(Schedule schedule) {
        return scheduleRepository.save(schedule);
    }

    public void delete(Schedule schedule){
        scheduleRepository.delete(schedule);
    }
    public void delete(Integer id){
        Schedule schedule=new Schedule();
        schedule.setId(id);
        scheduleRepository.delete(schedule);
    }
}
